package com.linln.modules.member.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.repository.MemberAgentRepository;
import com.linln.modules.member.service.MemberAgentService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class MemberAgentServiceImpl implements MemberAgentService {

    @Autowired
    private MemberAgentRepository memberAgentRepository;
    

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public MemberAgent getById(Long id) {
        return memberAgentRepository.findById(id).orElse(null);
    }
   
    /**
     * 查询用户的信息
     * @param MemberId
     * @param area
     * @return
     */
    public MemberAgent getByMemberIdAndArea(Long memberId,String area) {
    	 return memberAgentRepository.getByMemberIdAndArea(memberId,area);
    }
     
    
    
   @Override
    public   Boolean delCollect(Long id){
	   boolean isOk=false;
	   try{
		   memberAgentRepository.deleteById(id);
        isOk=true;
	   }catch(Exception e){
		   
	   }
       return isOk;
   }
    
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MemberAgent> getPageList(Example<MemberAgent> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        Page<MemberAgent> all = memberAgentRepository.findAll(example, page);
        List<MemberAgent> content = all.getContent();
        for (MemberAgent memberAgent :content
             ) {
            if(memberAgent.getAllmoney() == null){
                memberAgent.setAllmoney(0d);
            }
        }
        return all;
    }

    /**
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
   public  List<MemberAgent> getList(Example<MemberAgent> example){
    	return memberAgentRepository.findAll(example);
    }
    
    /**
     * 保存数据
     * @param MemberZan 实体对象
     */
    @Override
    public MemberAgent save(MemberAgent MemberZan) {
        return memberAgentRepository.save(MemberZan);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return memberAgentRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
    
    @Override
    @Transactional
	public  Boolean updateCurmoney(Double money, Long id) {
        return memberAgentRepository.updateCurmoney(money, id) > 0;

    }
    @Override
    @Transactional
	public  Boolean updateAgentStatus(Integer agentStatus,Long id) {
        return memberAgentRepository.updateAgentStatus(agentStatus, id) > 0;

    }
    
    public Integer updatePayStatus( String orderId) {
    	return memberAgentRepository.updatePayStatus(orderId);
    }

    public Integer updateRefundStatus( String orderId) {
    	return memberAgentRepository.updateRefundStatus(orderId);
    }
    
    /**
     * 增加金额
     */
    public Integer addMoney(Double money, String area) {
    	//增加个记录
    	return memberAgentRepository.addMoney(money,area);

    }

    /**
     * 退款减少金额
     */
    public Integer decMoney(Double money, String area) {
    	//增加个记录
    	return memberAgentRepository.decMoney(money,area);

    }

    @Override
    public long getAllCount(Example<MemberAgent> example) {
        return memberAgentRepository.count(example);
    }

    @Override
    public Double getMemberAgentSum() {
        return memberAgentRepository.getMemberAgentSum();
    }
    @Override
    public Integer getMemberAgentCount() {
        return memberAgentRepository.getMemberAgentCount();

    }
    
    /**
     * 获取分页列表数据
     * @param agent 实体对象
     * @return 返回分页数据
     */
    @Override
    @Transactional
    public Page<MemberAgent> getPageList(MemberAgent agent) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest(Sort.Direction.ASC);

        // 使用Specification复杂查询
        return memberAgentRepository.findAll(new Specification<MemberAgent>(){

            @Override
            public Predicate toPredicate(Root<MemberAgent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> preList = new ArrayList<>();
                
               
                if(agent.getMember()!=null){
                    // 联级查询部门
                    MemberInfo member = agent.getMember();

                    Join<MemberAgent, MemberInfo> join = root.join("member", JoinType.INNER);
                     cb.equal(join.get("id").as(Long.class), member.getId());
                     if(member.getNickname() != null){
                         preList.add(cb.like(join.get("nickname").as(String.class), "%"+ member.getNickname() + "%"));

                     }
                     if(member.getMobile()!= null){
                         preList.add(cb.like(join.get("mobile").as(String.class), "%"+ member.getMobile() + "%"));
                     }
                }

                // 数据状态
                    preList.add(cb.equal(root.get("status").as(Byte.class),StatusEnum.OK.getCode()));
                

                Predicate[] pres = new Predicate[preList.size()];
                return query.where(preList.toArray(pres)).getRestriction();
            }

        }, page);
    }


    @Override
    public MemberAgent getByArea(String area) {
        return memberAgentRepository.getByArea(area);
    }
}